package com.alibaba.www;

import java.util.Scanner;

public class ThroeableTest2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学生姓名");
        String name = scanner.next();
        while (true) {
            try {
                System.out.println("请输入学生年龄");
                int age = scanner.nextInt();
                System.out.println(new Student(name, age));
                break;
            } catch (RuntimeException e) {
                System.out.println("输入年龄不合理");
                continue;
            }
        }


    }
}

class Student {

    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        if (age >= 18 && age <= 25) {
            this.age = age;
        } else {
            throw new RuntimeException();

        }
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {

        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public int getAge() {

        return age;
    }

    public void setAge(int age) {
        if (age >= 18 && age <= 25) {
            this.age = age;
        } else {
            throw new NullPointerException();

        }

    }

}
